package com.zsrt.exam.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.zsrt.exam.entity.UserEntity;

public class UserService {
	
	
	public Map<String, List<UserEntity>> findAllUsersByProvince(List<UserEntity> userList) {
		
		Map<String, List<UserEntity>> map  = new HashMap<String, List<UserEntity>>();
		
		
		//拿到所有省份的set
		Set<String> provinceSet = new HashSet<String>();
		
		for(UserEntity user: userList){
			provinceSet.add(user.getProvince());
		}
		
		System.out.println(provinceSet);
		
		
		
		Iterator<String> it = provinceSet.iterator();
		
		while(it.hasNext()){
			String province = it.next();
			//保存的是某个省份下面的所有的用户 广东省 3
			List<UserEntity> provinceUsersList = new ArrayList<UserEntity>();
			
			
			
			for(UserEntity user: userList){
				
				if(user.getProvince().equals(province)){
					
					int score = user.getTotal();
					if(score>=500 && score<=600){
						provinceUsersList.add(user);
					}
				}
			}
			
			Collections.sort(provinceUsersList);
			map.put(province, provinceUsersList);
		}
		
		//....
		
		return map;
	}
	
	
	/**
	 * 查询全国各个省份的状元
	 * @param userList
	 * @return
	 */
	public List<UserEntity> findProvinceWinners(List<UserEntity> userList) {
		//遍历userList 取到1个具体的云南神 省份 要不要加? 遍历 provinceList
		//List<String>  provinceList 广东省  湖南省
		
		Set<String> provinceSet = new HashSet<String>();
		
		
		for(int index =0; index<userList.size();index++){
			UserEntity u = userList.get(index);
			String province = u.getProvince();
			provinceSet.add(province);
		}
		
		
		
		//省份列表provinceList找到之后。遍历省份 根据省份下面所有的用户
		
		
		List<UserEntity> zhangyuanlangs = new ArrayList<UserEntity>();
		
		int provinceCounter =provinceSet.size();
		//System.out.println(provinceCounter);
		
		
		Iterator<String> it = provinceSet.iterator();
		while(it.hasNext()){
			
			//1 provinceUserList 某个省下面的所有用户
			List<UserEntity> provinceUserList = new ArrayList<UserEntity>();
			String province = it.next();
			
			for(int j=0 ;j<userList.size();j++){
				UserEntity user =userList.get(j);
				
				if(user.getProvince().equals(province)){
					provinceUserList.add(user);
				}
			}
			
			//2 从某个省下面的所有用户去找状元
			UserEntity[] zy = new UserEntity[1];
			zy[0]=provinceUserList.get(0);
			
			for(int xx =1;xx<provinceUserList.size();xx++){
				if(provinceUserList.get(xx).getTotal() > zy[0].getTotal() ){
					zy[0] =provinceUserList.get(xx);
				}
			}
			
			zhangyuanlangs.add(zy[0]);
		}

		return zhangyuanlangs;
	}

	/**
	 * 查询本市的状元
	 * @param card
	 * @param userList
	 * @return
	 */
	public UserEntity findWinnerOfLocalCity(UserEntity user, List<UserEntity> userList) {
		//第1个已知的条件： 告诉我当前用户user  ==> city 
		
		String city = user.getCity();
		//第2个已知的条件： 告诉我所有的用户列表 userList  ==>根据city查找所有符合条件的列表 zhuangyuanList  sort
		
		
		UserEntity[] zy = new UserEntity[1];
		zy[0]=user;
		
		for(int index =0; index<userList.size();index++){
			
			UserEntity u = userList.get(index);
			
			if(city.equals(u.getCity())){
				int total = u.getTotal();
				
				if(total > zy[0].getTotal()){
					
					//如果u的总分 大于原来的总分   状元宝座就让你做。
					zy[0]=u;
				}
			}
			
			
		}
		
		
		return zy[0];
	}
	
	/**
	 * 去用户列表中去找看是否有相同的用户 如果有就登陆成功，否则登陆失败
	 * @param name
	 * @param password
	 * @param userList
	 * @return
	 */
	public UserEntity loginUser(String card, String password, List<UserEntity> userList) {
		
		for(int index =0 ;index<userList.size(); index++){
			UserEntity  user = userList.get(index);
			if(card.equals(user.getCard()) && password.equals(user.getPassword())){
				return user;
			}
		}
		
		return null;
	}
	
	
	
	/**
	 * 根据城市查找这个城市下面有多个个用户
	 * @param searchCity  城市
	 * @param userList    查找的范围
	 * @return  有多个个用户
	 */
	public List<UserEntity> findUsersByCity(String searchCity, List<UserEntity> userList) {
		List<UserEntity> searchList = new ArrayList<UserEntity>();
		
		for(UserEntity user : userList){
			String city = user.getCity();
			if(city.equals(searchCity)){
				searchList.add(user);
			}
		}
		return searchList;
	}
	
	
	/**
	 * 此处演示了方法的标准注释。在工作中和实际的项目中需要这么使用
	 * 根据准考证号码删除1个学生
	 * @param searchCard      准考证考吗
	 * @param userList        所有的考生的集合
	 * @return                true 删除成功 否则删除失败
	 */
	public boolean deleteUserByCard(String searchCard, List<UserEntity> userList) {
		boolean flag = false;
		for(int index =0; index<userList.size();index++){
			UserEntity user = userList.get(index);
			String card = user.getCard();
			if(card.equals(searchCard)){
				flag = userList.remove(user);
				break;
			}
		}
		return flag;
	}
	
	
	/**
	 * 管理员的2级主菜单
	 */
	public void getMenuAdminMain(){
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t        欢迎登陆高考成绩自主查询系统        ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          11 录入考生信息                 ");
		System.err.println("\t\t          12 修改考生信息             ");
		System.err.println("\t\t          13 删除考生信息                       ");
		System.err.println("\t\t          14 查询考生信息                       ");
		System.err.println("\t\t          15 查询所有考生信息");
		System.err.println("\t\t —————————————————————————————");

	}
	
	public void getUserMenu2(){
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t        欢迎登陆高考成绩自主查询系统        ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          21 我的信息                 ");
		System.err.println("\t\t          22 查询本市(登录用户所在的城市)的总分状元             ");
		System.err.println("\t\t          23 查询全国各个省份的状元                       ");
		System.err.println("\t\t          24 查询指定城市的状元                     ");
		System.err.println("\t\t          25 查询各个省份500<=x<=600分的总人数并由高到低排列显示");
		System.err.println("\t\t          26 查询各个省份x>600超过600分的总人数并由低到高排列显示");
		System.err.println("\t\t —————————————————————————————");

	}
	
	/**
	 * 管理员的3级菜单
	 */
	public void getMenuAdminSub(){
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t        欢迎登陆高考成绩自主查询系统        ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          141 根据户籍所在地查询                ");
		System.err.println("\t\t          142 根据姓名查询             ");
		System.err.println("\t\t          143 根据准考证号码查询                       ");
		System.err.println("\t\t          144 返回上一层");
		System.err.println("\t\t —————————————————————————————");
		
	}
	public void login(){
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t        欢迎登陆高考成绩自主查询系统        ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          1.管理员登录                 ");
		System.err.println("\t\t          2.普通会员登录              ");
		System.err.println("\t\t          3. 退   出                       ");
		System.err.println("\t\t —————————————————————————————");
		
	}
	
	/**
	 * 拿到1个系统管理员
	 * @return
	 */
	public UserEntity getAdmin(){
		UserEntity admin = new UserEntity();
		
		admin.setName("admin");
		
		//md5加密
		admin.setPassword("123456");
		
		//关键代码
		admin.setRole("admin");
		
		return admin;
	}
	
	
	public UserEntity addUser(String name,String card,int other){
		UserEntity user04 = new UserEntity();
		user04.setCard(card);
		user04.setName(name);
		user04.setPassword("123456");
		user04.setAge(24);
		user04.setProvince("湖南省");
		user04.setCity("邵阳市");
		user04.setPhone("18617067316");
		user04.setChinese(126);
		user04.setMath(117);
		user04.setEnghish(98);
		user04.setOther(other);
		user04.setRole("user");
		
		return user04;
	}
	
	/**
	 * 想办法搞出 6个普通用户。分2个省 每个省3个用户
	 */
	public List<UserEntity>  getAllUsers(){
		
		List<UserEntity> list = new ArrayList<UserEntity>();
		
		UserEntity user01 = new UserEntity();
		user01.setCard("07551001");
		user01.setName("刘浩");
		user01.setPassword("123456");
		user01.setAge(24);
		user01.setProvince("广东省");
		user01.setCity("茂名");
		user01.setPhone("15766472116");
		user01.setChinese(100);
		user01.setMath(100);
		user01.setEnghish(100);
		user01.setOther(205);
		user01.setRole("user");
		
		UserEntity user02 = new UserEntity();
		user02.setCard("07551002");
		user02.setName("张志东");
		user02.setPassword("123456");
		user02.setAge(24);
		user02.setProvince("广东省");
		user02.setCity("茂名");
		user02.setPhone("15766516891");
		user02.setChinese(100);
		user02.setMath(100);
		user02.setEnghish(100);
		user02.setOther(201);
		user02.setRole("user");
		
		UserEntity user03 = new UserEntity();
		user03.setCard("07551003");
		user03.setName("黄志杰");
		user03.setPassword("123456");
		user03.setAge(24);
		user03.setProvince("广东省");
		user03.setCity("茂名");
		user03.setPhone("15766518639");
		user03.setChinese(100);
		user03.setMath(100);
		user03.setEnghish(100);
		user03.setOther(210);
		user03.setRole("user");
		
		UserEntity user04 = new UserEntity();
		user04.setCard("07310001");
		user04.setName("陈皋");
		user04.setPassword("123456");
		user04.setAge(24);
		user04.setProvince("湖南省");
		user04.setCity("邵阳市");
		user04.setPhone("18617067316");
		user04.setChinese(100);
		user04.setMath(100);
		user04.setEnghish(100);
		user04.setOther(260);
		user04.setRole("user");
		
		UserEntity user05 = new UserEntity();
		user05.setCard("07556666");
		user05.setName("钟鹏");
		user05.setPassword("123456");
		user05.setAge(24);
		user05.setProvince("湖南省");
		user05.setCity("邵阳市");
		user05.setPhone("15766472116");
		user05.setChinese(100);
		user05.setMath(100);
		user05.setEnghish(100);
		user05.setOther(220);
		user05.setRole("user");
		
		UserEntity user06 = new UserEntity();
		user06.setCard("09996666");
		user06.setName("段誉");
		user06.setPassword("123456");
		user06.setAge(24);
		user06.setProvince("云南省");
		user06.setCity("大理市");
		user06.setPhone("15766472116");
		user06.setChinese(100);
		user06.setMath(100);
		user06.setEnghish(100);
		user06.setOther(220);
		user06.setRole("user");
		
		
		list.add(user01);
		list.add(user02);
		list.add(user03);
		list.add(user04);
		list.add(user05);
		list.add(user06);
		return list;
		
	}

	

	




	


	
	

}
